package vn.vfriends.payment.bean;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.payment.jpa.entity.Page;
import vn.vfriends.payment.jpa.entity.Role;
import vn.vfriends.payment.service.PageService;
import vn.vfriends.payment.service.RoleService;

/**
 *
 * @author tuan@vfriends.vn
 */
@Named
@ConversationScoped
public class PageBean implements Serializable {
    private final Logger logger = LoggerFactory.getLogger(PageBean.class);
    
    @Inject private PageService pageService;
    @Inject private RoleService roleService;
    @Inject private Conversation conversation;
    private Page[] selectedPages;
    private Page page;
    private List<Page> pageList;
    private List<Role> roleList;
    
    @PostConstruct
    public void init(){
        pageList = pageService.findPages();
        roleList = roleService.findRoles();
    }

    public List<Page> getPageList() {
        return pageList;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public Page getPage() {
        return page;
    }
    
    public Page getPage(String name){
        return this.pageService.findPageByName(name);
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public Page[] getSelectedPages() {
        return selectedPages;
    }

    public void setSelectedPages(Page[] selectedPages) {
        this.selectedPages = selectedPages;
    }
    
    public Conversation getConversation() {
        return conversation;
    }
    
    public void preNewAction() {
        page = new Page();
        
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    
    public String preViewAction() {
        String outcome = "";
        FacesContext context = FacesContext.getCurrentInstance();
        
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            
            if(this.page != null) {
                outcome = "view?faces-redirect=true";
            }
            else if (this.selectedPages == null || this.selectedPages.length == 0) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn một trang", ""));
            } else if (this.selectedPages.length > 1) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn chỉ một trang", ""));
            } else {
                this.page = this.selectedPages[0];
                outcome = "view?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
        return outcome;
    }
    
    public String preEditAction() {
        String outcome = null;
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            
            if(this.page != null){
                outcome = "edit?faces-redirect=true";
            } else if (this.selectedPages == null || this.selectedPages.length == 0) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn một trang", ""));
            } else if (this.selectedPages.length > 1) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn chỉ một trang", ""));
            } else {
                this.page = this.selectedPages[0];
                outcome = "edit?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));

        }
        return outcome;
    }
    
    public void preDeleteAction() {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            if ((this.page == null) && (this.selectedPages == null || this.selectedPages.length == 0)) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn trang", ""));
            }
        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }
    
    public String saveAction() {
        try {
            this.pageService.create(page);
            return "view?faces-redirect=true";
        } catch (Exception ex) {
            this.logger.error(ex.getMessage(), ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            return null;
        }
    }
    
    public String updateAction() {
        try {
            this.pageService.edit(page);
            return "view?faces-redirect=true";
        } catch (Exception ex) {
            this.logger.error(ex.getMessage(), ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            return null;
        }
    }

    public String cancelAction() {
        conversation.end();
        return "list?faces-redirect=true";
    }
    
    public void deleteAction() {
        try {
            if(this.page != null)
                this.pageService.destroy(this.page.getId());
            else
                for(Page selectedPage : selectedPages)                
                    this.pageService.destroy(selectedPage.getId());
            if(!conversation.isTransient())
                conversation.end();
            FacesContext.getCurrentInstance().getExternalContext().redirect("list.jsf");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }
}
